package com.example.booksdeal.basic;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.booksdeal.BmobDBO.BooksDetail;
import com.example.booksdeal.BmobDBO.User;
import com.example.booksdeal.R;
import com.example.booksdeal.util.DBOpenHelper;

import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SQLQueryListener;
import cn.bmob.v3.listener.UpdateListener;
import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class MycenterActivity extends AppCompatActivity {

    private DBOpenHelper dbOpenHelper;  //声明DBOpenHelper对象
    private TextView uid;
    private EditText nicheng,tel,dizhi;
    private String username;
    private String nickname,dianhua;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycenter);
        SQLiteStudioService.instance().start(this);
        dbOpenHelper = new DBOpenHelper(this,"db_booksdeal.db",null,1);  //实例化DBOpenHelper对象，用来创建数据库
        Bundle bundle = getIntent().getExtras();
        username = bundle.getString("username");
        findId();
        initView();
    }

    private void findId() {
        uid = findViewById(R.id.uid);
        nicheng = findViewById(R.id.nicheng);
        tel = findViewById(R.id.tel);
        dizhi = findViewById(R.id.dizhi);
    }

    private void initView() {
        uid.setText(username);

        BmobQuery<User> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("username", username);
        categoryBmobQuery.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> object, BmobException e) {
                if (e == null) {
                    nicheng.setText(object.get(0).getNickname());
                    tel.setText(object.get(0).getTelephone());
                }
            }
        });
//        BmobQuery<User> query = new BmobQuery<User>();
//        String bql = "select * from User where objectId = ?";
//        query.setPreparedParams(new Object[]{User.getCurrentUser(User.class).getObjectId()});
//     //   query.setPreparedParams(new Object[]{User.getCurrentUser(User.class)});
//        query.setSQL(bql);
//        query.doSQLQuery(new SQLQueryListener<User>() {
//            @Override
//            public void done(BmobQueryResult<User> bmobQueryResult, BmobException e) {
//                if (e == null) {
//                    List<User> object = (List<User>) bmobQueryResult.getResults();
//                    if (object != null && object.size() > 0) {
//                        nicheng.setText(object.get(0).getNickname());
//                        tel.setText(object.get(0).getTelephone());
//                    }
//                }
//            }
//        });
//        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
//        Cursor cursor = db.query("UserInfo", null, "username=?", new String[]{username}, null, null, null);
//        while (cursor.moveToNext()) {
//            nickname = cursor.getString(cursor.getColumnIndex("nickname"));
//            dianhua = cursor.getString(cursor.getColumnIndex("telephone"));
//        }
    }
}
